package com.example.hello.controller;

import com.example.hello.controller.request.AddOrderRequest;
import com.example.hello.model.Order;
import com.example.hello.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/order")

public class OrderController {

    @Autowired
    private IOrderService orderService;
    @PostMapping("/")
    public Order addNewOrder(@RequestBody AddOrderRequest addOrderRequest){
        log.info("订单信息{}",addOrderRequest);
        Order order = new Order();
        BeanUtils.copyProperties(addOrderRequest,order);
        orderService.addNewOrder(order,addOrderRequest.getItems());

        return order;
    }
    //get /order/张
    @GetMapping("/{customer}")
    public List<Order> getOrderByCustomer(@PathVariable String customer){
        return orderService.getOrderByCustomer(customer);
    }
}
